/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.reminder;

import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import researchgrants.parts.panels.*;
import java.awt.Component;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import researchgrants.parts.GrantRequest.GrantRequest;
import researchgrants.parts.date.DateForDb;
import researchgrants.utils.StringUtils;

/**
 * A compound panel to display and edit Perennial Stipends, which is a field (expectedPerennialStipends) in GrantRequest and a field (actualPerennialStipends) in GrantRequestStatusChangeAwarded.
 * This component is made of a year editable field, and a repeating (as the number of years) DirectMoney and IndirectMoney fields which are encompassed in PanelsStipend.
 * 
 * Two majorly different behaviors occur:
 * If a new PerennialStipends: All panels are initialized with defaults. Upon changes within any inner panel (year, yearly' panel, components within yearly' panels), global changes occur (validation for allowing or disallowing save, resizing years). Then when save occurs (when everything is valid only), this panel saves first and later all yearly panels save.
 * If an existing PerennialStipends: All panels are initialized with the previously saved values. The years' panels are resized upon change to the year field. Saving occurs independendtly for each field.
 * @author DOStudent1
 */
public class PanelsReminder extends PanelsCompoundView {


    // a reference to the reminder.
    // Can be initialized for editing an existing PerennialStipends be uninitialized for a new PerennialStipends.
    private Reminder reminder=null;
    private boolean isDataInitialized=false;


    /**
     * A reference for grantRequest.
     * Used for populating default values for the reminder and to associate the reminder to.
     */
    private GrantRequest grantRequest=null;

    // a global-style components
    private JPanel pnlContent=new JPanel(new BorderLayout(0, 0));
    
    // can set the default data of the panels using an existing GrantRequest
    // Examines the content of GrantRequest and init the defaults in accordance.
    
    
    /**
     * set mandatory data
     * @param grantRequest the <code>GrantRequest</code> to associate a <code>Reminder</code> to
     */
    public void setMandatoryData(GrantRequest grantRequest) {
            this.grantRequest=grantRequest;
    }
    
    // set this panels to an existing reminder
    public void setData(Reminder reminder) {
        if (reminder!=null) {
            this.reminder=reminder;
            isDataInitialized=true;
        }
    }
    
    private void unsetData() {
        if (reminder==null) {
            throw new RuntimeException("Can't unset data. No previous data available");
        }
        this.reminder=null;
        isDataInitialized=false;
    }
    
    // Gets the panel to display and edit the PerennialStipends
    @Override
    public Component getComponent() {
        redrawContent();
        return(pnlContent);
    }

    // puts the entire content 
    private void redrawContent() {
        pnlContent.removeAll();

        if (isDataInitialized) {
            JPanel pnlTopContent=new JPanel(new BorderLayout(5, 0));
            JPanel pnlTopLeftContent=new JPanel(new BorderLayout(0, 0));
            pnlTopLeftContent.add(new JLabel("Reminding on: " + DateForDb.fromDate(reminder.getCurrentDate()).toDisplayString()), BorderLayout.NORTH);
            JLabel lblReasonHeader=new JLabel("Reason:");     
            lblReasonHeader.setVerticalAlignment(SwingConstants.TOP);
            pnlTopLeftContent.add(lblReasonHeader, BorderLayout.WEST);
            pnlTopLeftContent.add(new JLabel("<html>" + StringUtils.encodeHTML(reminder.getCurrentNotes()) + "</html>"), BorderLayout.CENTER);
            pnlTopContent.add(pnlTopLeftContent, BorderLayout.WEST);
            
            JButton btnEditReminder=new JButton("Edit");
            btnEditReminder.setToolTipText("<html>Please don't edit the reminder if you want set a brand new reminder.<br>Instead, just cancel this reminder and create a new one.<br>If you just want to correct a minute error, edit is admissable.</html>");
            btnEditReminder.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    reminder.show(getParentWindow());
                    redrawContent();
                }
            });
            pnlTopContent.add(btnEditReminder, BorderLayout.EAST);
            pnlContent.add(pnlTopContent, BorderLayout.NORTH);
                    
            JButton btnCancelReminder=new JButton("Cancel Reminder");
            btnCancelReminder.setToolTipText("<html>Only one reminder per grant request is allowed.<br>Cancel this reminder to schedule a new one.</html>");
            btnCancelReminder.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    reminder.cancelReminder(grantRequest);
                    unsetData();
                    redrawContent();
                }
            });
            pnlContent.add(btnCancelReminder, BorderLayout.CENTER);
        } else {
            pnlContent.add(new JLabel("No reminder set."), BorderLayout.NORTH);
            if (grantRequest!=null) {
                JButton btnNewReminder=new JButton("Set Reminder");
                btnNewReminder.addActionListener(new ActionListener() {
                
                    public void actionPerformed(ActionEvent e) {
                        Reminder newReminder = Reminder.inputNew(getParentWindow(), grantRequest);
                        if (newReminder!=null) {
                            setData(newReminder);
                            redrawContent();
                        }
                    }
                });
                pnlContent.add(btnNewReminder, BorderLayout.SOUTH);
            } else {
                pnlContent.add(new JLabel("Save the grant request to set a reminder"), BorderLayout.SOUTH);
            }
        }
        getParentWindow().pack();
    }

    
    // retreives whether the panel is valid
    @Override
    public boolean isValid() {
        return(true);
    }
    

    // returns the wrapped Reminder - Used after saving a new Reminder
    public Reminder getReminder() {
        return(reminder);
    }
}
